package com.example.nightmeel.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TimeLineDbDao {

    public static final String TABLE_NAME = "timelinetable";// テーブル名

    // カラム情報
    public static final String COL_ID = "rowid";// ID
    public static final String COL_USER_ID = "user_id";// ユーザID
    public static final String COL_USER_NAME = "user_name";// ユーザ名
    public static final String COL_IMAGE = "image";// 画像
    public static final String COL_IMAGE_SIZE = "image_size";// 画像サイズ（大or小）
    public static final String COL_GEOTAG = "geotag";// ジオタグ
    public static final String COL_MESSAGE = "message";// メッセージ
    public static final String COL_CATEGORY = "category";// カテゴリー
    public static final String COL_ADVERTISEMENT_FLG = "advertisement_flg";// 広告フラグ
    public static final String COL_GOOD_CNT = "good_cnt";// いいねカウンタ
    public static final String COL_SEASON = "season";// 季節（春,夏,秋,冬）
    public static final String COL_DAYTIME = "daytime";// 時間帯（朝,昼,夜）
    public static final String COL_LASTUPDATE = "lastupdate";// 更新日
    public static final String COL_USER_ICON = "user_icon";// ユーザアイコン


    // カラム名管理
    private static final String[] COLUMNS = { COL_ID, COL_USER_ID, COL_USER_NAME, COL_IMAGE,
        COL_IMAGE_SIZE, COL_GEOTAG, COL_MESSAGE, COL_CATEGORY, COL_ADVERTISEMENT_FLG,
        COL_GOOD_CNT, COL_SEASON, COL_DAYTIME, COL_LASTUPDATE, COL_USER_ICON };

    private SQLiteDatabase db;// dbオブジェクト

    // インストラクタ
    public TimeLineDbDao(SQLiteDatabase db) {
        this.db = db;
    }

    // 挿入メソッド
    public long insert(TimeLineData timeLineData) {
        ContentValues values = new ContentValues();
        values.put(COL_USER_ID, timeLineData.getUser_id());
        values.put(COL_USER_NAME, timeLineData.getUser_name());
        values.put(COL_IMAGE, timeLineData.getImage());
        values.put(COL_IMAGE_SIZE, timeLineData.getImage_size());
        values.put(COL_GEOTAG, timeLineData.getGeotag());
        values.put(COL_MESSAGE, timeLineData.getMessage());
        values.put(COL_CATEGORY, timeLineData.getCategory());
        values.put(COL_ADVERTISEMENT_FLG, timeLineData.getAdvertisement_flg());
        values.put(COL_GOOD_CNT, timeLineData.getGood_cnt());
        values.put(COL_SEASON, timeLineData.getSeason());
        values.put(COL_DAYTIME, timeLineData.getDaytime());
        values.put(COL_LASTUPDATE, timeLineData.getLastupdate());
        values.put(COL_USER_ICON, timeLineData.getUser_icon());
        return db.insert(TABLE_NAME, null, values);
    }

    // 更新メソッド
    public int update(TimeLineData timeLineData) {
        ContentValues values = new ContentValues();
        String whereClause = "rowid = " + timeLineData.getRowid();
        values.put(COL_USER_ID, timeLineData.getUser_id());
        values.put(COL_USER_NAME, timeLineData.getUser_name());
        values.put(COL_IMAGE, timeLineData.getImage());
        values.put(COL_IMAGE_SIZE, timeLineData.getImage_size());
        values.put(COL_GEOTAG, timeLineData.getGeotag());
        values.put(COL_MESSAGE, timeLineData.getMessage());
        values.put(COL_CATEGORY, timeLineData.getCategory());
        values.put(COL_ADVERTISEMENT_FLG, timeLineData.getAdvertisement_flg());
        values.put(COL_GOOD_CNT, timeLineData.getGood_cnt());
        values.put(COL_SEASON, timeLineData.getSeason());
        values.put(COL_DAYTIME, timeLineData.getDaytime());
        values.put(COL_LASTUPDATE, timeLineData.getLastupdate());
        values.put(COL_USER_ICON, timeLineData.getUser_icon());
        return db.update(TABLE_NAME, values, whereClause, null);
    }

    //全検索メソッド
    public List<TimeLineData> selectAll() {
        List<TimeLineData> timeLineDataList = new ArrayList<TimeLineData>();
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, COL_LASTUPDATE + " DESC");// 更新日でソート(降順)
        while (cursor.moveToNext()) {
            TimeLineData timeLineData = new TimeLineData();
            timeLineData.setRowid(cursor.getInt(0));
            timeLineData.setUser_id(cursor.getInt(1));
            timeLineData.setUser_name(cursor.getString(2));
            timeLineData.setImage(cursor.getString(3));
            timeLineData.setImage_size(cursor.getInt(4));
            timeLineData.setGeotag(cursor.getString(5));
            timeLineData.setMessage(cursor.getString(6));
            timeLineData.setCategory(cursor.getInt(7));
            timeLineData.setAdvertisement_flg(cursor.getInt(8));
            timeLineData.setGood_cnt(cursor.getInt(9));
            timeLineData.setSeason(cursor.getInt(10));
            timeLineData.setDaytime(cursor.getInt(11));
            timeLineData.setLastupdate(cursor.getString(12));
            timeLineData.setUser_icon(cursor.getString(13));
            timeLineDataList.add(timeLineData);

        
        }
        return timeLineDataList;
    }

    // ID検索メソッド
    public TimeLineData selectById(int rowId) {
        String selection = "rowid = " + rowId;
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, selection, null, null, null, null);
        while (cursor.moveToNext()) {
            TimeLineData timeLineData = new TimeLineData();
            timeLineData.setRowid(cursor.getInt(0));
            timeLineData.setUser_id(cursor.getInt(1));
            timeLineData.setUser_name(cursor.getString(2));
            timeLineData.setImage(cursor.getString(3));
            timeLineData.setImage_size(cursor.getInt(4));
            timeLineData.setGeotag(cursor.getString(5));
            timeLineData.setMessage(cursor.getString(6));
            timeLineData.setCategory(cursor.getInt(7));
            timeLineData.setAdvertisement_flg(cursor.getInt(8));
            timeLineData.setGood_cnt(cursor.getInt(9));
            timeLineData.setSeason(cursor.getInt(10));
            timeLineData.setDaytime(cursor.getInt(11));
            timeLineData.setLastupdate(cursor.getString(12));
            timeLineData.setUser_icon(cursor.getString(13));

            return timeLineData;
        }
        return null;
    }

    // 削除（ID検索）メソッド
    public int deleteById(int rowId) {
        return db.delete(TABLE_NAME, "rowid = " + rowId, null);
    }

    // 全削除メソッド
    public int deleteAll(int rowId) {
        return db.delete(TABLE_NAME, null, null);
    }
}
